//
//  JSModelManager.m
//  JigSaw
//
//  Created by slim on 15/1/26.
//  Copyright (c) 2015年 LITB. All rights reserved.
//

#import "JSModelManager.h"
#import "JSNetworking.h"
#import "JSGetModelListRequest.h"
#import "zipzap.h"
#import "NSDictionary+Json.h"

@implementation JSModelManager
MACRO_SHARED_INSTANCE_IMPLEMENTATION(JSModelManager)

- (instancetype)init
{
    self = [super init];
    if (self) {
        NSString* bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Models.bundle"];
        self.modelBundlePath = [NSBundle bundleWithPath:bundlePath].bundlePath;
        NSFileManager* fmgr = [NSFileManager defaultManager];
        NSArray* files = [fmgr contentsOfDirectoryAtPath:self.modelBundlePath error:nil];
        self.stencils = [@[] mutableCopy];
        for (NSString* fileName in files) {
            NSString* jsonFilePath = [[self.modelBundlePath stringByAppendingPathComponent:fileName] stringByAppendingPathComponent:@"stencil.json"];
            NSData* jsonData = [fmgr contentsAtPath:jsonFilePath];
            NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            NSDictionary* dic = [NSDictionary dictionaryWithJsonString:jsonString];
            JSStencilItemDesc* desc = [self parseDictToStencilDesc:dic];
            if (desc) {
                [self.stencils addObject:desc];
            }
        }
    }
    return self;
}

- (NSArray*)getProperModelByImageCount:(NSInteger)count
{
    NSMutableArray* temp = [@[] mutableCopy];
    for (JSStencilItemDesc* desc in self.stencils) {
        if (desc.bestNum == count) {
            [temp addObject:desc];
        }
    }
//    UIImage* image = nil;
//    switch (count) {
//        case 1:
//            image = [UIImage imageNamed:@"journey_1"];
//            break;
//        case 2:
//            image = [UIImage imageNamed:@"youandme_2"];
//            break;
//        case 3:
//            image = [UIImage imageNamed:@"food_3"];
//            break;
//        case 4:
//            image = [UIImage imageNamed:@"qingxin_4"];
//            break;
//        default:
//            break;
//    }
//    if (image) {
//        [temp addObject:image];
//    }
    return temp;
}

- (JSStencilItemDesc*)parseDictToStencilDesc:(NSDictionary*)dic
{
    JSStencilItemDesc* desc = [JSStencilItemDesc instanceWithSetAttributesBlock:^(id aSelf) {
        JSStencilItemDesc* tmpDesc = aSelf;
        DICT_GET_OBJECT(tmpDesc.stencilItems,  @"stencilItems", dic);
        NSNumber* bestNum = nil;
        DICT_GET_OBJECT(bestNum,               @"bestNum", dic);
        tmpDesc.bestNum = bestNum.intValue;
        DICT_GET_OBJECT(tmpDesc.thumbnailPath, @"thumbnailPath", dic);
        NSNumber* index = nil;
        DICT_GET_OBJECT(index,                 @"index", dic);
        tmpDesc.index = index.intValue;
    }];
    return desc;
}

- (JSStencilItem*)parseDictToStencilItem:(NSDictionary*)dic
{
    JSStencilItem* item = [JSStencilItem instanceWithSetAttributesBlock:^(id aSelf) {
        JSStencilItem* tmpItem = aSelf;
        DICT_GET_OBJECT(tmpItem.type,              @"type", dic);
        DICT_GET_OBJECT(tmpItem.imgFileName,       @"imgFileName", dic);
        DICT_GET_OBJECT(tmpItem.defaultText,       @"defaultText", dic);
        DICT_GET_OBJECT(tmpItem.defaultFont,       @"defaultFont", dic);
        DICT_GET_OBJECT(tmpItem.defaultTextColor,  @"defaultTextColor", dic);
        DICT_GET_OBJECT(tmpItem.defaultTextSize,   @"defaultTextSize", dic);
        DICT_GET_OBJECT(tmpItem.x,                 @"x", dic);
        DICT_GET_OBJECT(tmpItem.y,                 @"y", dic);
        DICT_GET_OBJECT(tmpItem.height,            @"height", dic);
        DICT_GET_OBJECT(tmpItem.width,             @"width", dic);
        DICT_GET_OBJECT(tmpItem.gravity,           @"gravity", dic);
        DICT_GET_OBJECT(tmpItem.imgIndex,          @"imgIndex", dic);
        DICT_GET_OBJECT(tmpItem.zorder,            @"zorder", dic);
        DICT_GET_OBJECT(tmpItem.maskFileName,      @"maskFileName", dic);
        DICT_GET_OBJECT(tmpItem.backgroudColor,    @"backgroudColor", dic);
        DICT_GET_OBJECT(tmpItem.dateFormat,        @"dateFormat", dic);
    }];
    return item;
}

- (PGJSImageItem*)getImageItem
{
    PGJSImageItem* item = [PGJSImageItem instanceWithSetAttributesBlock:^(id aSelf) {
        PGJSImageItem* item = aSelf;
        item.thumbnail = [UIImage imageNamed:@"small.jpg"];
        item.previewImage = [UIImage imageNamed:@"preview.jpg"];
        item.orgImage = [UIImage imageNamed:@"org.jpg"];
    }];
    return item;
}

- (void)getModelList
{
    JSGetModelListRequest* request = [JSGetModelListRequest instanceWithSetAttributesBlock:^(id aSelf) {
        JSGetModelListRequest* tmprequest = aSelf;
    }];
    [[[JSNetworking sharedInstance] rac_signalForHttpGET:request] subscribeNext:^(id x) {
        
    } error:^(NSError *error) {
        
    }];
}

- (NSDictionary*)getCurrentStencilItems
{
    for (JSStencilItemDesc* desc in self.stencils) {
        if (desc.index == self.selectedModelIndex) {
            return desc.stencilItems;
        }
    }
    return nil;
}

- (UIImage*)getThumbnailImage:(NSInteger)index
{
    for (JSStencilItemDesc* desc in self.stencils) {
        if (desc.index == index) {
            NSString* imagePath = [self.modelBundlePath stringByAppendingPathComponent:desc.thumbnailPath];
            return [UIImage imageWithContentsOfFile:imagePath];
        }
    }
    return nil;
}
@end
